//
//  CardCollectionViewCell.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/12.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "CardCollectionViewCell.h"
#import "CardlistTableViewCell.h"
#import "CardHeaderView.h"

@interface CardCollectionViewCell ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray * leftTitArr;
    UITableView * inforListTableView;
}
@property (nonatomic,assign)NSInteger selfRowNumber;
@property (nonatomic,strong) CardHeaderView * headerView;
@end

@implementation CardCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        leftTitArr = @[@"品名",@"材质",@"规格",@"钢厂",@"采购数量",@"最高价格设置"];
        inforListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth-kHeightProportion_In_iPhoneTriploid(30), kHeightProportion_In_iPhoneTriploid(360)) style:UITableViewStylePlain];
        inforListTableView.layer.cornerRadius = kHeightProportion_In_iPhoneTriploid(6);
        inforListTableView.showsVerticalScrollIndicator = NO;
        inforListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        inforListTableView.estimatedRowHeight = 0;
        inforListTableView.estimatedSectionHeaderHeight = 0;
        inforListTableView.estimatedSectionFooterHeight = 0;
        inforListTableView.layer.masksToBounds = YES;
        inforListTableView.delegate = self;
        inforListTableView.dataSource = self;
        inforListTableView.scrollEnabled = NO;
        inforListTableView.backgroundColor = [UIColor whiteColor];
        inforListTableView.tableFooterView = [UIView new];
        inforListTableView.tableHeaderView = [self createCardheaderView];
        inforListTableView.rowHeight = kHeightProportion_In_iPhoneTriploid(44);
        [self addSubview:inforListTableView];
        //
        GLGWButton * addOneBut = [[GLGWButton alloc] initWithTitle:@"再加一条" titleLableFont:SYSTEMFONT(14) titleLableColor:kColor_Rule_Black inSuperView:self];
        addOneBut.image_Name = @"icon_zengjia";
        addOneBut.backgroundColor = [UIColor whiteColor];
        addOneBut.layer.cornerRadius = kHeightProportion_In_iPhoneTriploid(22);
        addOneBut.layer.shadowColor = [UIColor colorWithHexString:@"#000000"].CGColor;//shadowColor阴影颜色
        addOneBut.layer.shadowOffset = CGSizeMake(0, 0);//shadowOffset阴影偏移，默认(0, -3),这个跟shadowRadius配合使用
        addOneBut.layer.shadowOpacity = 0.1;////阴影透明度，默认0
        addOneBut.layer.shadowRadius = 3.0;////阴影半径，默认3
        [addOneBut setImagePositionWithType:SSImagePositionTypeLeft spacing:kWidthProportion_In_iPhoneTriploid(10)];
        [addOneBut mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self).offset(0);
            make.width.equalTo(@(self->inforListTableView.width-kWidthProportion_In_iPhoneTriploid(110)));
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(44)));
        }];
        
        [[addOneBut rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            kKeyboardEndEditing
            if (self.delegate && [self.delegate respondsToSelector:@selector(addOneCardSelect:)]) {
                [self.delegate addOneCardSelect:self.selfRowNumber];
            }
        }];
    }
    return self;
}

-(UIView *)createCardheaderView{
    self.headerView = [[CardHeaderView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(52))];
    kWeakSelf(self);
    self.headerView.Block_Delete = ^{
        if (weakself.delegate && [weakself.delegate respondsToSelector:@selector(deleteOne:)]) {
            [weakself.delegate deleteOne:weakself.selfRowNumber];
        }
    };
    return self.headerView;
}

#pragma mark -- set
-(void)setCard_inforModel:(CardInforModel *)card_inforModel{
    _card_inforModel = card_inforModel;
    [inforListTableView reloadData];
}
#pragma mark  ---- UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CardlistTableViewCell * cell = [CardlistTableViewCell cellWithTableView:tableView];
    switch (indexPath.row) {
        case 4:
        case 5:{
            cell.changeTextField.userInteractionEnabled = YES;
            if (indexPath.row == 4) {
                cell.unitStr = @"吨";
                cell.changeTextField.placeholder = @"请填写数量";
                cell.changeTextField.keyboardType = UIKeyboardTypeNumberPad;

                cell.changeTextField.text = self.card_inforModel.weight>0?[NSString stringWithFormat:@"%ld",self.card_inforModel.weight]:@"";
                @weakify(self);
                [cell.changeTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
                    @strongify(self);
                     self.card_inforModel.weight =[x integerValue];
                }];
               
            }else{
                cell.unitStr = @"元/吨";
                cell.changeTextField.keyboardType = UIKeyboardTypeDecimalPad;
                cell.changeTextField.placeholder = @"请填写最高价格(选填)";
                cell.changeTextField.text = self.card_inforModel.topPrice>0?[NSString stringWithFormat:@"%.2f",self.card_inforModel.topPrice]:@"";
                @weakify(self);
                [cell.changeTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
                    @strongify(self);
                    self.card_inforModel.topPrice =[x floatValue];
                }];
            }
        }
            break;
        case 0:{
            cell.changeTextField.text = _card_inforModel.productName;
        }
            break;
        case 1:{
            cell.changeTextField.text = _card_inforModel.materialName;
        }
            break;
        case 2:{
            cell.changeTextField.text = _card_inforModel.specificationsName;
        }
            break;
        case 3:{
            cell.changeTextField.text = _card_inforModel.factoryName;
        }
            break;
            
        default:
            
            break;
    }
    if (indexPath.row !=4 || indexPath.row !=5) {
        cell.showPush = YES;
    }
    cell.c_type = CellType_CardList;
    cell.nameLeftStr = leftTitArr[indexPath.row];
    [cell setSepartionLineWith:lineModel_Half];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(chickRowNum:farthRowNum:)]) {
        [self.delegate chickRowNum:indexPath.row farthRowNum:self.selfRowNumber];
    }
}

#pragma mark --- set
-(void)dataCount:(NSInteger)count theSelfNum:(NSInteger)s_row{
    self.selfRowNumber = s_row;
    [self.headerView indexAll:count nowIndex:s_row];
}
@end
